<?php
/**
 * 
 * 메인 controller
 * @author gudals
 *
 */

class contMainCt extends contController
{
    public $action = 'index';

    private $aCss = array(
                'name' => 'cont.main',
                'file_list' => array()
            );
    
    private $aJs = array(
                'name' => 'cont.main',
                'file_list' => array('util/spin', 'lib/jquery.form', 'cont/conts')
            );

    public function index()
    {
        // 각 페이지에 필요한 CSS, JS 파일만 적용할 것.
        commViewer::_css($this->aCss);
        commViewer::_js($this->aJs);
        
        // 레이아웃 표시 여부
        //$this->sTopLayoutView = false;
        //$this->sAsideLayoutView = false;
        //$this->sFooterLayoutView = false;
        
        $aParam = $this->params;
        
        $aParam['cate'] = empty($aParam['cate']) === false ? $aParam['cate'] : 1;
        
        // 총 건수
        $aRowT = m('contents_info')->getTotal($aParam);
        $iTotalCnt = $aRowT['cnt'];
        
        // 한페이지에 표시할 수
        $iPerPage    = $aParam['per_page'] ? $aParam['per_page'] : 20;
        // 총개수, 표시할 수, 페이징 변수명, 추가 파라미터
        $utilPager = new utilPager($iTotalCnt, $iPerPage, 'page', $aPagerParams);
        $aPager = $utilPager->getPaging();
        $iOffset = $aPager['offset'];

        // 컨텐츠 리스트
        $aContentsList = m('contents_info')->getList($aParam, $iOffset, $iPerPage);
        
        $aContentsNum = array();
        $aUserNum = array();
        for ($i = 0, $iLen = count($aContentsList); $i < $iLen; $i++) {
            $aContentsNum[] = $aContentsList[$i]['contents_num'];
            $aUserNum[] = $aContentsList[$i]['user_num'];
        }
        $aUserNum = array_unique($aUserNum);
        
        // 유저 정보
        $sUserNumList = (empty($aUserNum) === false) ? implode(',', $aUserNum) : '';
        if (empty($sUserNumList) === false) {
            $aRowsUser = m('user_info')->getInfoList($sUserNumList);
            $aUserInfoList = array();
            for ($i = 0, $iLen = count($aRowsUser); $i < $iLen; $i++) {
                $_row = $aRowsUser[$i];
                $aUserInfoList[$_row['user_num']] = $_row;
            }
        }
        
        $sContentsNumList = (empty($aContentsNum) === false) ? implode(',', $aContentsNum) : '';
        // 코멘트 리스트
        if (empty($sContentsNumList) === false) {
            $aRows = m('contents_comment')->getListByContentsNums($sContentsNumList);
            $aContentsComment = array();
            for ($i = 0, $iLen = count($aRows); $i < $iLen; $i++) {
                $_row = $aRows[$i];
                
                $aContentsComment[$_row['contents_num']][] = $_row;
            }
        }
        
        // 카테고리 정보
        $aCateList = m('cate_info')->getList('Y');

        // data assign
        $aAssignData = array(
            'params' => $aParam,
            'aContentsList' => $aContentsList,
            'aUserInfoList' => $aUserInfoList,
            'aContentsComment' => $aContentsComment,
            'aCateList' => $aCateList,
            'paging' => $aPager['all']
        );

        // display view
        $this->view('/cont/main/index.html', $aAssignData);
    }
    
    public function _imageRotate($filename, $degrees)
    {
        $source = imagecreatefromjpeg($filename);
        
        $rotate = imagerotate($source, $degrees, 0);
        
        imagejpeg($rotate, $filename);
        
        imagedestroy($source);
        imagedestroy($rotate);
        
        return $filename;
    }
    
    /**
     * 컨텐츠 등록
     * 
     */
    public function add()
    {
        $this->params['user_num'] = commSession::session_get_data('sUserNum');
        
        $bParam = contCommon::chkRequestValiable($this->params, array('user_num' => 'int', 'contents_desc' => 'string'));
        if ($bParam === false) {
            $aReturn = array('code' => 201, 'msg' => 'Parameter Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aParam = $this->params;
        $aParam['cate_num'] = $aParam['cate'];
        //$aParam['contents_desc'] = addslashes($aParam['contents_desc']);
        
        $aExif = exif_read_data($_FILES['uploads']['tmp_name'], 0, true);
        $ort = $aExif['IFD0']['Orientation'];
        
        $_ro = $ort == 3 ? 180 : (($ort == 6) ? -90 : (($ort == 8) ? 90 : 0));
        
        //$_fileName = $_FILES['uploads']['tmp_name'];
        if (empty($_ro) === false && $_ro != 0) {
            //$_fileName = $this->_imageRotate($_FILES['uploads']['tmp_name'], -90);
            $_FILES['uploads']['tmp_name'] = $this->_imageRotate($_FILES['uploads']['tmp_name'], -90);
        }
        
        // 파일 업로드
        $aParam['contents_img'] = '';
        if (empty($_FILES['uploads']['name']) === false) {
            $utilImages = new utilImages();
            $utilImages->setThumbnailMake(450);
            $aReturnFile = $utilImages->upload($_FILES['uploads'], 'conts');
            if ($aReturnFile[0] != 200) {
                $aReturn = array('code' => $aReturnFile[0],'msg' => $aReturnFile[1]);
                jsonEncodePrint($aReturn);
            }
            $aParam['contents_img'] = $aReturnFile[1];
        }
        
        $bResult = m('contents_info')->insert($aParam);
        if (!$bResult) {
            $aReturn = array('code' => 202, 'msg' => 'DB Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aReturn = array('code' => 200);
        jsonEncodePrint($aReturn);
    }
    
    /**
     * 코멘트 등록
     * 
     */
    public function addComment()
    {
        $this->params['user_num'] = commSession::session_get_data('sUserNum');
        
        $bParam = contCommon::chkRequestValiable($this->params, array('user_num' => 'int', 'contents_num' => 'int', 'comment_desc' => 'string'));
        if ($bParam === false) {
            $aReturn = array('code' => 201, 'msg' => 'Parameter Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aParam = $this->params;
        
        $bResult = m('contents_comment')->insert($aParam);
        if (!$bResult) {
            $aReturn = array('code' => 202, 'msg' => 'DB Error!');
            jsonEncodePrint($aReturn);
        }
        
        $aReturn = array('code' => 200);
        jsonEncodePrint($aReturn);
    }
    
    public function commentList()
    {
        // 레이아웃 표시 여부
        $this->sTopLayoutView = false;
        $this->sAsideLayoutView = false;
        $this->sFooterLayoutView = false;
        
        $iContentsNum = $this->params['contents_num'];
        $aCommentList = m('contents_comment')->getList($iContentsNum);
        
        // data assign
        $aAssignData = array(
            'name' => 'gudals',
            'aCommentList' => $aCommentList
        );
        
        // display view
        $sCommentListHtml = $this->view('/cont/main/comment_list.html', $aAssignData, true);
        
        $aReturn = array('code' => 200, 'msg' => '', 'res' => $sCommentListHtml);
        jsonEncodePrint($aReturn);
    }
}